using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace HtmlReport
{
    public class HtmlReportDocument : System.Windows.Forms.WebBrowser
    {
        public event EventHandler OnDataSourceChanged;
        public event EventHandler OnPageIndexChanged;
        public event EventHandler OnDocumentShowed;
        private int _PageHeight=0;
        public int PageHeight{
            get{
                return _PageHeight;
            }
            set
            {
                _PageHeight = value;
            }
        }
        public HtmlReportDocument()
        {
            report = new HtmlReportExporter();
            IsCompleted = false;
            this.Navigate("about:blank");
            while (!IsCompleted)
            {
                Application.DoEvents();
                System.Threading.Thread.Sleep(5);
            }
            this.DocumentText = "<html><head></head><body></body></html>";
        }
        private bool IsCompleted = false;
        HtmlReportExporter report;
        protected override void OnNavigated(System.Windows.Forms.WebBrowserNavigatedEventArgs e)
        {
            IsCompleted = true;
        }
        public static string TmpDir
        {
            set
            {
                HtmlReportExporter.TmpDir = value;
            }
            get
            {
                return HtmlReportExporter.TmpDir;
            }
        }
        public DataSet DataSource
        {
            set
            {
                bool changed = report.DataSource != value;
                report.DataSource = value;
                if (changed && (OnDataSourceChanged != null))
                {
                    OnDataSourceChanged(this, new EventArgs());
                }
            }
            get
            {
                return report.DataSource;
            }
        }
        public void Load(string FileName)
        {
            report.TempateFile = FileName;
        }
        public void ExportToWord(string FileName, bool OpenFileInNewWindow)
        {
            System.IO.StreamWriter strmWriter = System.IO.File.CreateText(FileName);
            strmWriter.Write(Content);
            strmWriter.Close();
            if (OpenFileInNewWindow)
            {
                System.Diagnostics.Process.Start(FileName);
            }
        }
        private string Content = "";
        private int _CurrentIndex = 0;
        public string ReportContent
        {
            get
            {
                return Content;
            }
        }
        public int PageIndex
        {
            get
            {
                return _CurrentIndex;
            }
            set
            {
                if (_CurrentIndex != value)
                {
                    ShowReport(value);
                }
            }
        }
        public int PageCount
        {
            get
            {
                return report.PageCount;
            }
        }
        public void MoveFirst()
        {
            ShowReport(0);
        }
        public void MoveNext()
        {
            if (_CurrentIndex < report.PageCount - 1)
            {
                ShowReport(_CurrentIndex + 1);
            }
        }
        public void MovePrevious()
        {
            if (_CurrentIndex > 0)
            {
                ShowReport(_CurrentIndex - 1);
            }
        }
        public void MoveLast()
        {
            if (_CurrentIndex != report.PageCount - 1)
            {
                ShowReport(report.PageCount - 1);
            }
        }
        public void ShowReport()
        {
            Content = report.ExportReport();
            _CurrentIndex = 0;
            ShowReport(_CurrentIndex);
            if (OnDocumentShowed != null)
            {
                OnDocumentShowed(this, new EventArgs());
            }
            if (_PageHeight > 0)
            {
                this.Document.Window.Size  = new System.Drawing.Size(this.Document.Window.Size.Width, _PageHeight);
            }
        }
        public void ShowReport(int PageIndex)
        {
            if (PageIndex >= report.PageCount)
                PageIndex = report.PageCount - 1;
            if (PageIndex < 0) PageIndex = 0;
            List<string> arraysContent = new List<string>();
            int indexStart = 0;
            int indexEnd = 0;
            string strSplit = Tags.BEGINSCRIPT + Tags.ENDPAGE_BOOKMARK + Tags.ENDSCRIPT;
            do
            {
                indexEnd = indexStart;
                indexStart = Content.IndexOf(strSplit, indexEnd);
                if (indexStart < 0)
                {
                    arraysContent.Add(Content.Substring(indexEnd));
                    break;
                }
                arraysContent.Add(Content.Substring(indexEnd, indexStart - indexEnd));
                indexStart += strSplit.Length;
            }
            while (true);
            if (_CurrentIndex != PageIndex)
            {
                _CurrentIndex = PageIndex;
                OnPageIndexChanged(this, new EventArgs());
            }
            string strPre = "";
            string strEnd = "";
            if (_CurrentIndex > 0)
            {
                strPre = string.Join("\r\n", arraysContent.GetRange(0, _CurrentIndex).ToArray());
            }
            if (_CurrentIndex < arraysContent.Count - 1)
            {
                strEnd = string.Join("\r\n", arraysContent.GetRange(_CurrentIndex + 1, arraysContent.Count - _CurrentIndex - 1).ToArray());
            }
            if (arraysContent.Count > _CurrentIndex)
            {
                this.DocumentText = HideDiv(strPre) + ShowDiv(arraysContent[_CurrentIndex]) + HideDiv(strEnd);
            }
            else
            {
                this.DocumentText = HideDiv(strPre) + ShowDiv(arraysContent[arraysContent.Count - 1]) + HideDiv(strEnd);
            }
        }
        public List<string> GetPagesContent()
        {
            List<string> arraysContent = new List<string>();
            int indexStart = 0;
            int indexEnd = 0;
            string strSplit = Tags.BEGINSCRIPT + Tags.ENDPAGE_BOOKMARK + Tags.ENDSCRIPT;
            do
            {
                indexEnd = indexStart;
                indexStart = Content.IndexOf(strSplit, indexEnd);
                if (indexStart < 0)
                {
                    arraysContent.Add(Content.Substring(indexEnd));
                    break;
                }
                arraysContent.Add(Content.Substring(indexEnd, indexStart - indexEnd));
                indexStart += strSplit.Length;
            }
            while (true);

            return arraysContent;
        }
        private string HideDiv(string st)
        {
            return st.Replace(Tags.PAGE_STYLE, "div_printer_hide");
        }
        private string ShowDiv(string st)
        {
            return st.Replace(Tags.PAGE_STYLE, "div_printer_show");
        }
        public void PrintPreview()
        {

            this.ShowPrintPreviewDialog();
        }
        public void PrintReport()
        {
            this.ShowPrintDialog();
        }
        public void ClearParameter()
        {
            report.Parameters.Clear();
        }
        public Dictionary<string, object> Parameters
        {
            get
            {
                return report.Parameters;
            }
        }
        public void AddParameter(string ParameterName, object value)
        {
            if (!report.Parameters.ContainsKey(ParameterName))
            {
                report.Parameters.Add(ParameterName, value);
            }
            else
            {
                report.Parameters[ParameterName] = value;
            }
        }
    }

}
